package chapter05

import scala.annotation.tailrec

/**
 * 递归实现
 * 阶乘
 */
object Demo10_TestRecursion {
  def main(args: Array[String]): Unit = {
    println(fact(5))
    println(tailFact(5))
  }
  def fact(i: Int): Int = {
    if (i == 0) return 1
    fact( i - 1) * i
  }

  //尾递归实现
  def tailFact(n: Int): Int = {
    @tailrec
    def loop(n: Int , currRes: Int) : Int = {
      if(n == 0) return currRes
      loop(n - 1 , currRes * n)
    }
    loop(n , 1)
  }
}
